import { unref } from 'vue';
import { defHttp } from '/@/utils/http/axios';
import { useMessage } from '/@/hooks/web/useMessage';

const { createConfirm } = useMessage();

export enum Api {
  queryAreaTreeSync = '/admin/hardware/area/query',
  queryArea = '/admin/hardware/area/query',
  save = '/admin/hardware/area/add',
  edit = '/admin/hardware/area/edit',
  delete = '/admin/hardware/area/delete',
  deleteBatch = '/admin/hardware/area/deleteBatch',
  exportXlsUrl = '/admin/hardware/area/exportXls',
  importExcelUrl = '/admin/hardware/area/importExcel',

  queryGeoJson = '/admin/hardware/area/geo_json/query',
  saveGeoJson = '/admin/hardware/area/geo_json/save',

  getCurrentUserDeparts = '/sys/user/getCurrentUserDeparts',
  selectDepart = '/sys/selectDepart',
  getUpdateDepartInfo = '/sys/user/getUpdateDepartInfo',
  doUpdateDepartInfo = '/sys/user/doUpdateDepartInfo',
  changeDepartChargePerson = '/sys/user/changeDepartChargePerson',
}

/**
 * 获取地块树列表
 */
export const queryAreaTreeSync = (params?) => {
  if (!params) {
    params = {}
  }
  params.resultType = 'tree'
  return defHttp.get({ url: Api.queryAreaTreeSync, params })
};
/**
 * 获取地块
 */
export const queryArea = (params?) => {
  if (!params) {
    params = {}
  }
  return defHttp.get({ url: Api.queryArea, params })
};

/**
 * 保存或者更新地块角色
 */
export const saveOrUpdateArea = (params, isUpdate) => {
  if (isUpdate) {
    return defHttp.put({ url: Api.edit, params });
  } else {
    return defHttp.post({ url: Api.save, params });
  }
};

/**
 * 批量删除地块角色
 */
export const deleteBatchArea = (params, confirm = false) => {
  return new Promise((resolve, reject) => {
    const doDelete = () => {
      resolve(defHttp.delete({ url: Api.deleteBatch, params }, { joinParamsToUrl: true }));
    };
    if (confirm) {
      createConfirm({
        iconType: 'warning',
        title: '删除',
        content: '确定要删除吗？',
        onOk: () => doDelete(),
        onCancel: () => reject(),
      });
    } else {
      doDelete();
    }
  });
};

/**
 * 查询地块地图信息
 */
export const queryGeoJson = (params?) => defHttp.get({ url: Api.queryGeoJson, params });
/**
 * 保存地块地图信息
 */
export const saveGeoJson = (params) => defHttp.post({ url: Api.saveGeoJson, params });

/**
 * 获取登录用户地块信息
 */
export const getUserDeparts = (params?) => defHttp.get({ url: Api.getCurrentUserDeparts, params });
/**
 * 切换选择地块
 */
export const selectDepart = (params?) => defHttp.put({ url: Api.selectDepart, params });

/**
 * 编辑地块前获取地块相关信息
 * @param id
 */
export const getUpdateDepartInfo = (id) => defHttp.get({ url: Api.getUpdateDepartInfo, params: { id } });

/**
 * 编辑地块
 * @param params
 */
export const doUpdateDepartInfo = (params) => defHttp.put({ url: Api.doUpdateDepartInfo, params });

/**
 * 删除地块
 * @param id
 */
export const deleteDepart = (id) => defHttp.delete({ url: Api.delete, params: { id } }, { joinParamsToUrl: true });
